#include <QtGui>
// #include <qt.cpp>
#include "tsc_dlg.h"
#include <pthread.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <printl.h>

#ifdef __cplusplus
extern "C" {
#endif


int xres;
int yres;
struct pt  screen_pt[5];
struct pt  adc_pt[5];
struct pt  current_pt;


static pthread_t g_tsc;
extern tsc_dlg *g_dlgmsg;
#define DEF_DEVICE_TOUCH      "/dev/input/event2"
char *dev_touch    = DEF_DEVICE_TOUCH;			// 默认设备
pthread_mutex_t qtui_mutex;

static int sort_by_x(const void *a, const void *b)
{
	return (((struct pt *)a)->x - ((struct pt *)b)->x);
}
static int sort_by_y(const void *a, const void *b)
{
	return (((struct pt *)a)->y - ((struct pt *)b)->y);
}
/*
                            type code value
00091d0 2ce9 0000 3899 000a 0003 0018 0001 0000    触屏按下  特点 18开始

00091e0 2ce9 0000 d3fe 000a 0003 0001 0acb 0000    y坐标    type = 3 code = 1
00091f0 2ce9 0000 d41e 000a 0003 0000 0663 0000    x坐标    type = 3 code = 0
0009200 2ce9 0000 d43d 000a 0000 0000 0000 0000    同步     全 0

0009210 2ce9 0000 703e 000b 0003 0001 0ac3 0000
0009220 2ce9 0000 705e 000b 0003 0000 0664 0000
0009230 2ce9 0000 707d 000b 0000 0000 0000 0000

0009260 2ce9 0000 a8be 000c 0003 0001 0abc 0000
0009270 2ce9 0000 a8de 000c 0003 0000 0663 0000
0009280 2ce9 0000 a8fd 000c 0000 0000 0000 0000

00093c0 2cea 0000 583e 0002 0003 0018 0000 0000   结束  特点 code=18 value = 0
00093d0 2cea 0000 583e 0002 0000 0000 0000 0000   同步  全 0
*/
void while_unempty(int fdinput)
{
#define MAX_SAMP (40)
	struct input_event input_press;
	int len;
	struct pt samp[MAX_SAMP];
	int i_samp = 0;


	input_press.code = 0;

	while(1) {
		len = read(fdinput, &input_press, sizeof(input_press));
		if (len != -1) {
			// 读取y
			if (input_press.type == 3 && input_press.code == 1) {
				samp[i_samp].y = input_press.value;
				// current_pt.y = input_press.value;
			}
			// 读取x
			else if (input_press.type == 3 && input_press.code == 0) {
				samp[i_samp].x = input_press.value;
				i_samp++;
				// current_pt.x = input_press.value;
			}

			else if (input_press.code == 0x18 && i_samp > 10) {
				dbg("touch up \n");

				break;
			}
			if (i_samp >= MAX_SAMP) {
				i_samp = 0;
			}
		}
	}


#ifndef NDEBUG
	printf("i_samp = %d\n", i_samp);
	for (int i = 0; i < i_samp; i++) {
		printf("x = %d y %d\n", samp[i].x, samp[i].y);
	}
#endif

	qsort(samp, i_samp, sizeof(struct pt), sort_by_x);
	current_pt.x  = samp[i_samp / 2].x;

	qsort(samp, i_samp, sizeof(struct pt), sort_by_y);
	current_pt.y  = samp[i_samp / 2].y;

	// 设备后面还有部分没有用的数据
	read(fdinput, &input_press, sizeof(input_press));
	read(fdinput, &input_press, sizeof(input_press));
	read(fdinput, &input_press, sizeof(input_press));
}

void *th_read_event(void *arg)
{
	// printf("000\n");
	// sleep(3);


	int fd_touch = 0;
	int len;
	struct input_event input_press;
	int pt_index = 0;

	fd_touch = open(dev_touch, O_RDONLY | O_NONBLOCK);
	if (fd_touch < 0) {
		printf("fail open touch %s\n", dev_touch);
		return 0;
	}
	while(pt_index < 5) {
		len = read(fd_touch, &input_press, sizeof(input_press));

		if (len != -1) {
			dbg(" type %4.4x code %4.4x  val %4.4x\n",
			    input_press.type,
			    input_press.code,
			    input_press.value
			   );

			dbg("touch press\n");
			if (input_press.code == 0x18 && input_press.value == 0x0001) {
				while_unempty(fd_touch);
			}
			dbg("touch x %x y %x\n", current_pt.x, current_pt.y);

			adc_pt[pt_index].x = current_pt.x;
			adc_pt[pt_index].y = current_pt.y;
			pt_index++;

			g_dlgmsg->do_update();
		}
	}

}
tsc_dlg::tsc_dlg(QWidget *parent)
	: QDialog(parent)
{

	pthread_mutex_init(&qtui_mutex, NULL);
	int frameStyle = 0;//= QFrame::Sunken | QFrame::Panel;


	// 获取屏幕大小
	QDesktopWidget *desktopWidget = QApplication::desktop();
	QRect screenRect = desktopWidget->screenGeometry();

	xres = screenRect.width();
	yres = screenRect.height();




	// 计算5个校准点位置
	screen_pt[0].x = 50,         screen_pt[0].y = 50;
	screen_pt[1].x = xres - 50,  screen_pt[1].y = 50;
	screen_pt[2].x = xres - 50,  screen_pt[2].y = yres - 50;
	screen_pt[3].x = 50,         screen_pt[3].y = yres - 50;
	screen_pt[4].x = xres / 2,   screen_pt[4].y = yres / 2;


	// 设置待会要显示的校准点位置
	ts_index = 0;
	draw_cross(ts_index, screen_pt[ts_index].x, screen_pt[ts_index].y);



	// 设置窗口全屏
	this->setFixedSize(xres  , yres);


	// 顶层显示
	setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
	pthread_create(&g_tsc, NULL, th_read_event, NULL);

	// 以下内容只为调试
#if 0
	QGridLayout *layout = new QGridLayout;


	layout->setColumnStretch(1, 1);
	layout->setColumnMinimumWidth(1, 250);


	QPushButton *btn_ok =    new QPushButton("OK");
	layout->addWidget(btn_ok	, 4, 2);



	connect(btn_ok, SIGNAL(clicked()), this, SLOT(set_net()));
	setLayout(layout);

	setWindowTitle(("Standard Dialogs"));
#endif
}

int a = 0;

void tsc_dlg::do_update()
{
	ts_index++;
	if (ts_index >= 5) {
		// hide();
		this->close();
	}
	draw_cross(ts_index, screen_pt[ts_index].x, screen_pt[ts_index].y);
	pthread_mutex_lock(&qtui_mutex);
	QWidget::update();
	pthread_mutex_unlock(&qtui_mutex);
}
void tsc_dlg::draw_cross(int index, int x, int y)
{
	ts_index = index;
	cross_x = x;
	cross_y = y;
}
void tsc_dlg::paintEvent(QPaintEvent *event)
{
	pthread_mutex_lock(&qtui_mutex);
	
	// 绘制十字
	QPainter paint(this);

	paint.setBrush(Qt::gray);
	paint.drawRect(rect());

	QPainter paint_blue(this);
	paint_blue.setPen(Qt::red);

	paint.drawLine(cross_x - 20, cross_y, cross_x + 20, cross_y);
	paint.drawLine(cross_x, cross_y - 20, cross_x, cross_y + 20);


	pthread_mutex_unlock(&qtui_mutex);
}

void tsc_dlg::set_net()
{

}


void tsc_dlg::doShow()
{
	printf("doShow\n");
	ts_index++;
	draw_cross(ts_index, screen_pt[ts_index].x, screen_pt[ts_index].y);
	QWidget::update();

}




#ifdef __cplusplus
}
#endif